package drone.basis.util ;

import java.util.HashMap ;
import java.util.Map ;

import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSONObject ;
import com.hikvision.artemis.sdk.ArtemisHttpUtil ;
import com.hikvision.artemis.sdk.config.ArtemisConfig ;

public class CameraUtil
{

	public static final String ARTEMIS_PATH = "/artemis" ;

	public static final String previewURLsApi = ARTEMIS_PATH + "/api/video/v2/cameras/previewURLs" ;

	public static final String captureURLsApi = ARTEMIS_PATH + "/api/video/v1/manualCapture" ;

	public static final String controllingURLsApi = ARTEMIS_PATH + "/api/video/v1/ptzs/controlling" ;

	public static final String LEFT = "LEFT" ;// 左转

	public static final String RIGHT = "RIGHT" ;// 右转

	public static final String UP = "UP" ;// 上转

	public static final String DOWN = "DOWN" ;// 下转

	public static final String ZOOM_IN = "ZOOM_IN" ;// 焦距变大

	public static final String ZOOM_OUT = "ZOOM_OUT" ;// 焦距变小

	public static final String LEFT_UP = "LEFT_UP" ;// 左上

	public static final String LEFT_DOWN = "LEFT_DOWN" ;// 左下

	public static final String RIGHT_UP = "RIGHT_UP" ;// 右上

	public static final String RIGHT_DOWN = "RIGHT_DOWN" ;// 右下

	public static final String FOCUS_NEAR = "FOCUS_NEAR" ;// 焦点前移

	public static final String FOCUS_FAR = "FOCUS_FAR" ;// 焦点后移

	public static final String IRIS_ENLARGE = "IRIS_ENLARGE" ;// 光圈扩大

	public static final String IRIS_REDUCE = "IRIS_REDUCE" ;// 光圈缩小

	public static final String WIPER_SWITCH = "WIPER_SWITCH" ;// 接通雨刷开关

	public static final String START_RECORD_TRACK = "START_RECORD_TRACK" ;// 开始记录轨迹

	public static final String STOP_RECORD_TRACK = "STOP_RECORD_TRACK" ;// 停止记录轨迹

	public static final String START_TRACK = "START_TRACK" ;// 开始轨迹

	public static final String STOP_TRACK = "STOP_TRACK" ;// 停止轨迹；

	public static String capture (
			String host ,
			String key ,
			String secret ,
			String index
	)
	{

		ArtemisConfig.host = host ;
		ArtemisConfig.appKey = key ;
		ArtemisConfig.appSecret = secret ;

		Map < String , String > path = new HashMap < String , String > ( ) ;

		path.put ( "https://" , captureURLsApi ) ;

		JSONObject jsonBody = new JSONObject ( ) ;
		jsonBody.put ( "cameraIndexCode" , index ) ;

		String body = jsonBody.toJSONString ( ) ;

		System.out.println ( "海康视频抓图host : " + host + " 编码 : " + index ) ;

		try
		{
			String result = ArtemisHttpUtil.doPostStringArtemis ( path , body , null , null , "application/json" , null ) ;
			System.out.println ( "视频抓图结果 : " + result ) ;
			JSONObject object = JSON.parseObject ( result ) ;
			JSONObject data = object.getJSONObject ( "data" ) ;
			String url = data.getString ( "picUrl" ) ;
			return url ;
		} catch ( Exception e )
		{
			System.out.println ( "抓图异常 :" + e.getMessage ( ) ) ;
			return StringUtil.EMPTY ;
		}

	}

	public static String preview (
			String host ,
			String key ,
			String secret ,
			String index ,
			String type
	)
	{

		ArtemisConfig.host = host ;
		ArtemisConfig.appKey = key ;
		ArtemisConfig.appSecret = secret ;

		Map < String , String > path = new HashMap < String , String > ( ) ;

		path.put ( "https://" , previewURLsApi ) ;

		JSONObject jsonBody = new JSONObject ( ) ;
		jsonBody.put ( "cameraIndexCode" , index ) ;
		jsonBody.put ( "streamType" , 0 ) ;
		jsonBody.put ( "protocol" , type ) ;
		jsonBody.put ( "transmode" , 1 ) ;
		jsonBody.put ( "expand" , "transcode=1&videotype=h264" ) ;
		jsonBody.put ( "streamform" , "ps" ) ;

		String body = jsonBody.toJSONString ( ) ;

		System.out.println ( "海康视频监控转换host : " + host + " 编码 : " + index ) ;

		try
		{

			String result = ArtemisHttpUtil.doPostStringArtemis ( path , body , null , null , "application/json" , null ) ;
			System.out.println ( "视频监控转换结果 : " + result ) ;
			JSONObject object = JSON.parseObject ( result ) ;
			JSONObject data = object.getJSONObject ( "data" ) ;
			String url = data.getString ( "url" ) ;
			return url ;
		} catch ( Exception e )
		{
			System.out.println ( "视频监控转换异常 :" + e.getMessage ( ) ) ;
			return StringUtil.EMPTY ;
		}

	}

	public static String control (
			String host ,
			String key ,
			String secret ,
			String index ,
			String action ,
			String command ,
			String speed
	)
	{

		ArtemisConfig.host = host ;
		ArtemisConfig.appKey = key ;
		ArtemisConfig.appSecret = secret ;

		Map < String , String > path = new HashMap < String , String > ( ) ;

		path.put ( "https://" , controllingURLsApi ) ;

		JSONObject jsonBody = new JSONObject ( ) ;
		jsonBody.put ( "cameraIndexCode" , index ) ;
		jsonBody.put ( "action" , action ) ;
		jsonBody.put ( "command" , command ) ;
		jsonBody.put ( "speed" , speed ) ;

		String body = jsonBody.toJSONString ( ) ;

		System.out.println ( "海康视频监控控制host : " + host + " 编码 : " + index ) ;

		try
		{

			String result = ArtemisHttpUtil.doPostStringArtemis ( path , body , null , null , "application/json" , null ) ;
			System.out.println ( "视频监控控制结果 : " + result ) ;
			JSONObject object = JSON.parseObject ( result ) ;
			return object.getString ( "code" );
		} catch ( Exception e )
		{
			System.out.println ( "视频监控转换异常 :" + e.getMessage ( ) ) ;
			return StringUtil.EMPTY ;
		}

	}

	public static void main (
			String [ ] args
	)
	{
		String result = preview ( "222.174.42.86:1443" , "25346899" , "sJmaAluxH2QZYO6RddPj" , "c7d46a4e6afe47e4a1b6d91db83dba45" , "hlss" ) ;
		System.out.println ( "result结果示例: " + result ) ;

		String result1 = capture ( "222.174.42.86:1443" , "25346899" , "sJmaAluxH2QZYO6RddPj" , "c7d46a4e6afe47e4a1b6d91db83dba45" ) ;
		System.out.println ( "result1结果示例: " + result1 ) ;
		//

	}
}
